package com.example.dell.intelligentagriculture.fragment;



import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dell.intelligentagriculture.R;


public class HelpFragment extends Fragment implements View.OnClickListener {
    View view;
    private TextView zhiyin1, zhiyin2, zhiyin3, zhiyin4;
    private ImageView zhiyinImg1, zhiyinImg2, zhiyinImg3;
    LinearLayout content1, content2, content3, content4;
    int hidderViewHeight;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_help, null);
        initView();
        return view;
    }

    /**
     * 初始化控件
     */
    private void initView() {
        zhiyin1 = view.findViewById(R.id.zhiyin1);
        zhiyin2 = view.findViewById(R.id.zhiyin2);
        zhiyin3 = view.findViewById(R.id.zhiyin3);
        zhiyin4 = view.findViewById(R.id.zhiyin4);
        zhiyinImg1 = view.findViewById(R.id.zhiyinImg1);
        zhiyinImg2 = view.findViewById(R.id.zhiyinImg2);
        zhiyinImg3 = view.findViewById(R.id.zhiyinImg3);
        content1 = view.findViewById(R.id.content1);
        content2 = view.findViewById(R.id.content2);
        content3 = view.findViewById(R.id.content3);
        content4 = view.findViewById(R.id.content4);
        zhiyin1.setOnClickListener(this);
        zhiyin2.setOnClickListener(this);
        zhiyin3.setOnClickListener(this);
        zhiyin4.setOnClickListener(this);
        zhiyinImg1.setOnClickListener(this);
        zhiyinImg2.setOnClickListener(this);
        zhiyinImg3.setOnClickListener(this);

        //获取收集屏幕的尺寸，把手机的分辨率有dp给位px（像素）
        float density = getResources().getDisplayMetrics().density;
        hidderViewHeight = (int) (density * 160 + 0.5f);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zhiyin1:
            case R.id.zhiyinImg1:
                if (content1.getVisibility() == View.GONE) {
                    showView(content1);
                } else {
                    hideView(content1);
                }
                break;
            case R.id.zhiyin2:
            case R.id.zhiyinImg2:
                if (content2.getVisibility() == View.GONE) {
                    showView(content2);
                } else {
                    hideView(content2);
                }
                break;
            case R.id.zhiyin3:
            case R.id.zhiyinImg3:
                if (content3.getVisibility() == View.GONE) {
                    showView(content3);
                } else {
                    hideView(content3);
                }
                break;
            case R.id.zhiyin4:
                if (content4.getVisibility() == View.GONE) {
                    showView(content4);
                } else {
                    hideView(content4);
                }
                break;
        }
    }

    /**
     * 显示内容
     */
    private void showView(View view) {
        view.setVisibility(View.VISIBLE);
        ValueAnimator animatior = createAnimatior(0, hidderViewHeight, view);
        animatior.start();
    }

    private void hideView(final View view) {
        int height = view.getHeight();
        ValueAnimator animatior = createAnimatior(height, 0, view);
        animatior.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animatior.start();
    }

    private ValueAnimator createAnimatior(int start, int stop, final View view) {
        ValueAnimator animator = ValueAnimator.ofInt(start, stop);

        //动画监听器
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //获取动画的属性
                int value = (int) animation.getAnimatedValue();
                //获取要执行动画的控件的属性（宽高）
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.height = value;
                view.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
}
